/**
 * 2010(c) Copyright Oceansoft Information System Co.,LTD. All rights reserved.
 * <p>
 * Compile: JDK 1.6+
 * <p>
 * 版权所有(C)：江苏欧索软件有限公司
 * <p>
 * 公司名称：江苏欧索软件有限公司
 * <p>
 * 公司地址：中国苏州科技城青山路1号
 * <p>
 * 网址: http://www.oceansoft.com.cn
 * <p>
 * 版本: 苏州公安统一用户管理平台1.0
 * <p>
 * 作者: 孙炯
 * <p>
 * 文件名:SysOperMgrDaoImpl.java
 * <p>
 * 类产生时间: 2012-6-13 下午5:33:54
 * <p>
 * 负责人: 孙炯
 * 所在组 : 苏州公安统一用户管理平台
 * <p>
 * 所在部门: 电信/国土——技术二部
 * <p>
 * <p>
 */
package com.oceansoft.core.dao.impl;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.Reader;
import java.sql.Clob;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcTemplate;
import org.springframework.stereotype.Repository;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import com.oceansoft.core.common.util.PageUtil;
import com.oceansoft.core.common.util.Pagination;
import com.oceansoft.core.dao.ISysOperMgrDao;
import com.oceansoft.core.dao.mapper.SysOperMgrMapper;
import com.oceansoft.core.domain.SysOperLog;

/**
 * 系统操作日志接口实现类
 * 
 * @author sunj
 * 
 */
@Repository
public class SysOperMgrDaoImpl implements ISysOperMgrDao {
	@Resource
	private SimpleJdbcTemplate sjt;

	@Override
	public Pagination<SysOperLog> findOperInfo(int pageIndex) {
		String sql_list = PageUtil
				.getPageSQL(
						"SELECT T3.NAME AS DEPTNAME,T1.* FROM T_SYS_OPER_LOG T1,T_SYS_USER T2,T_SYS_DEPT T3 WHERE T1.USERID=T2.POLICENUM AND T2.DEPTID=T3.DEPTID ORDER BY OPERTIME",
						pageIndex);
		String sql_count = "SELECT COUNT(*) FROM T_SYS_OPER_LOG";
		int _total = sjt.queryForInt(sql_count);
		List<SysOperLog> list = sjt.query(sql_list, new SysOperMgrMapper());
		return new Pagination<SysOperLog>(list, _total, pageIndex);
	}

	@Override
	public Pagination<SysOperLog> findOperInfoByConditions(String userid, String uname, String module,
			String opertime1, String opertime2, int pageIndex) {
		StringBuilder sql_list = new StringBuilder(
				"SELECT T3.NAME AS DEPTNAME,T1.* FROM T_SYS_OPER_LOG T1,T_SYS_USER T2,T_SYS_DEPT T3 WHERE T1.USERID=T2.POLICENUM AND T2.DEPTID=T3.DEPTID");
		StringBuilder sql_count = new StringBuilder(
				"SELECT COUNT(*) FROM T_SYS_OPER_LOG T1,T_SYS_USER T2,T_SYS_DEPT T3 WHERE T1.USERID=T2.POLICENUM AND T2.DEPTID=T3.DEPTID");
		StringBuilder sql = new StringBuilder();
		if (StringUtils.hasText(userid)) {
			sql.append(" AND T1.userid LIKE  '%" + userid + "%'");
		}
		if (StringUtils.hasText(uname)) {
			sql.append(" AND T2.NAME LIKE  '%" + uname + "%'");
		}
		if (StringUtils.hasText(module)) {
			sql.append(" AND T1.module = " + module);
		}
		if (StringUtils.hasText(opertime1) && StringUtils.hasText(opertime2)) {
			sql.append(" AND trunc(T1.OPERTIME) BETWEEN TO_DATE('" + opertime1 + "','yyyy-MM-dd') AND TO_DATE('"
					+ opertime2 + " ','yyyy-MM-dd')");
		}
		int _total = sjt.queryForInt(sql_count.append(sql).toString());
		List<SysOperLog> list = sjt.query(
				PageUtil.getPageSQL(sql_list.append(sql).append(" ORDER BY OPERTIME DESC").toString(), pageIndex),
				new SysOperMgrMapper());
		return new Pagination<SysOperLog>(list, _total, pageIndex);
	}

	@Override
	public Pagination<SysOperLog> findOperInfoByConditions(String userid, String uname, String module, String snapdata,
			String opertime1, String opertime2, int pageIndex) {
		StringBuilder sql_list = new StringBuilder(
				"SELECT T3.NAME AS DEPTNAME,T1.* FROM t_sys_oper_log T1,T_SYS_USER T2,T_SYS_DEPT T3 WHERE T1.USERID=T2.POLICENUM AND T2.DEPTID=T3.DEPTID");
		StringBuilder sql_count = new StringBuilder(
				"SELECT COUNT(*) FROM t_sys_oper_log T1,T_SYS_USER T2,T_SYS_DEPT T3 WHERE T1.USERID=T2.POLICENUM AND T2.DEPTID=T3.DEPTID");
		StringBuilder sql = new StringBuilder();
		if (StringUtils.hasText(opertime1)) {
			sql.append(" AND TRUNC(T1.OPERTIME) >= TO_DATE('" + opertime1 + "','YYYY-MM-DD')");
		}
		if (StringUtils.hasText(opertime2)) {
			sql.append(" AND TRUNC(T1.OPERTIME) <= TO_DATE('" + opertime2 + "','YYYY-MM-DD')");
		}
		if (StringUtils.hasText(module)) {
			sql.append(" AND T1.MODULE = " + module);
		}
		if (StringUtils.hasText(userid)) {
			sql.append(" AND T1.USERID LIKE  '%" + userid + "%'");
		}
		if (StringUtils.hasText(uname)) {
			sql.append(" AND T2.NAME LIKE  '%" + uname + "%'");
		}
		if (StringUtils.hasText(snapdata)) {
			sql.append(" AND T1.SNAPDATA LIKE  '%" + snapdata + "%'");
		}
		//System.out.println(sql);
		int _total = sjt.queryForInt(sql_count.append(sql).toString());
		List<SysOperLog> list = sjt.query(
				PageUtil.getPageSQL(sql_list.append(sql).append(" ORDER BY OPERTIME DESC").toString(), pageIndex),
				new SysOperMgrMapper());
		return new Pagination<SysOperLog>(list, _total, pageIndex);
	}

	@Override
	public void insertSysOperLog(SysOperLog sysOperLog) {
		String sql = "INSERT INTO T_SYS_OPER_LOG(DBID,IP,OPERTIME,CONTENT,TITLE,USERID,UNAME,MODULE,MODNAME) VALUES(SEQ_SYS_OPER_LOG.NEXTVAL,?,SYSDATE,?,?,?,?,?,?)";
		sjt.update(sql, sysOperLog.getIp(), sysOperLog.getContent(), sysOperLog.getTitle(), sysOperLog.getUserid(),
				sysOperLog.getUname(), sysOperLog.getModule(), sysOperLog.getModname());
	}

	@Override
	public void saveLog(SysOperLog log) {
		String sql = "INSERT INTO T_SYS_OPER_LOG(DBID,OPERTIME,CONTENT,TITLE,USERID,UNAME,MODULE,MODNAME) VALUES(SEQ_SYS_OPER_LOG.NEXTVAL,SYSDATE,?,?,?,?,?,?)";
		sjt.update(sql, log.getContent(), log.getTitle(), log.getUserid(), log.getUname(), log.getModule(),
				log.getModname());
	}

	@Override
	public SysOperLog findOperInfoById(String logId) {
		List<SysOperLog> list = sjt.query("SELECT * FROM T_SYS_OPER_LOG T WHERE T.DBID=?", new RowMapper<SysOperLog>() {
			@Override
			public SysOperLog mapRow(ResultSet rs, int rowIndex) throws SQLException {
				SysOperLog log = new SysOperLog();
				log.setDbId(rs.getLong("DBID"));
				log.setIp(rs.getString("IP"));
				log.setOpertime(rs.getTimestamp("OPERTIME"));
				log.setOperResult(rs.getString("OPERRESULT"));
				log.setContent(rs.getString("CONTENT"));
				log.setUserid(rs.getString("USERID"));
				log.setUname(rs.getString("UNAME"));
				log.setModule(rs.getString("MODULE"));
				log.setModname(rs.getString("MODNAME"));
				log.setOperType(rs.getInt("OPERTYPE"));
				Clob clob = rs.getClob("SNAPDATA");
				if (null != clob) {
					StringBuffer sb = new StringBuffer();
					Reader reader = clob.getCharacterStream();
					BufferedReader br = new BufferedReader(reader);
					try {
						String s = br.readLine();
						while (null != s) {
							sb.append(s);
							s = br.readLine();
						}
						log.setSnapData(sb.toString());
					} catch (IOException e) {
						e.printStackTrace();
					}
				}
				return log;
			}
		}, logId);
		if (!CollectionUtils.isEmpty(list)) {
			return list.get(0);
		}
		return null;
	}
}
